import { _decorator, Component, Node, v2, v3, Vec2, Vec3, Animation, Prefab, instantiate, find } from 'cc';
import { EventManager } from '../event/EventManager';
import ResManager from '../common/ResManager';
import { PrefabEnum } from '../common/PrefabEnum';
import WorldManager from '../common/WorldManager';
const { ccclass, property } = _decorator;

@ccclass('Hero')
export class Hero extends Component {
    /**速度 */
    speed:number = 100

    /**方向 */
    dir:Vec2 = v2(0, 0)
    /**终点 */
    endPos:Vec2 = v2(0, 0)

    animation:Animation

    state:string

    /**
     * 创建出一个英雄
     * @param pos 出生点
     */
    static async createHero(pos:Vec3){
        let prefab = await ResManager.instance.loadAssetByUrl<Prefab>(PrefabEnum.Hero)
        let node = instantiate(prefab)
        find('Canvas').addChild(node)
        node.worldPosition = pos
    }

    /**
     * 根据村庄的X Y值创建一个英雄
     * @param x 
     * @param y 
     */
    static async createHeroByXY(x:number, y:number){
        let pos = WorldManager.instance.getWorldPostionByXY(v2(x, y))
        this.createHero(pos)
    }

    protected onLoad(): void {
        this.animation = this.node.getComponent(Animation)
    }

    start() {
        EventManager.instance.on(EventManager.EventType.HeroMove, this.moveToPostion, this)
    }

    protected onDisable(): void {
        EventManager.instance.off(EventManager.EventType.HeroMove, this.moveToPostion, this)
    }

    update(deltaTime: number) {
        
        if(this.dir.length() == 0){
            return
        }

        let dir3 = v3(this.dir.x, this.dir.y, 0)
        this.node.translate(dir3.multiplyScalar(this.speed * deltaTime))

        let wpos = this.node.worldPosition
        let distance = Vec2.distance(this.endPos,v2(wpos.x, wpos.y))
        if(distance < 10){
            this.playIdle()
        }
    }

    /**
     * 行走到指定的坐标
     * @param pos 
     */
    moveToPostion(pos:Vec3){
      
        let wpos = this.node.worldPosition
        this.endPos = v2(pos.x, pos.y)
        this.dir = this.endPos.clone().subtract(v2(wpos.x, wpos.y)).normalize()

        let hu = Math.atan2(this.dir.y, this.dir.x)
        // 角度= 弧度 * 180 / pi
        // 弧度= 角度 / 180 * pi
        let angle = hu * 180 / Math.PI

        if(-22.5 <= angle && angle <= 22.5 ){
            // 0 右
            this.animation.play('right')
            this.state = 'right'
        }
        if(22.5 <= angle && angle <= 67.5 ){
            // 45 右上
            this.animation.play('right_up')
            this.state = 'right_up'
        }
        if(67.5 <= angle && angle <= 112.5 ){
            // 90 上
            this.animation.play('up')
            this.state = 'up'
        }
        if(112.5 <= angle && angle <= 157.5 ){
            // 135 左上
            this.animation.play('left_up')
            this.state = 'left_up'
        }
        if(157.5 <= angle || angle <= -157.5){
            // 180 左
            this.animation.play('left')
            this.state = 'left'
        }
        if(-157.5 <= angle && angle <= -112.5 ){
            // -135 左下
            this.animation.play('left_down')
            this.state = 'left_down'
        }
        if(-112.5 <= angle && angle <= -67.5 ){
            // -90 下
            this.animation.play('down')
            this.state = 'down'
        }
        if(-67.5 <= angle && angle <= -22.5 ){
            // -45 右下
            this.animation.play('right_down')
            this.state = 'right_down'
        }
    }

    playIdle(){
        this.animation.play('idle_' + this.state)
        this.dir = v2(0, 0)

        
    }
}


